import { defineStore } from 'pinia'
import { getUserInfo } from '@/api/userInfo.js'

export const useUserInfoStore = defineStore('userInfo', {
    state: () => {
        return {
            imageUrl: '',
            name: '',
            sex: '',
            department: '',
            identity: '',
            account: '',
            email: '',
            id: ''
        }
    },
    actions: {
        async fetchUserInfo(id) {
            try {
                const res = await getUserInfo(id);
                this.imageUrl = res.data.results.image_url;
                this.name = res.data.results.name;
                this.sex = res.data.results.sex;
                this.department = res.data.results.department;
                this.identity = res.data.results.identity;
                this.account = res.data.results.account;
                this.email = res.data.results.email;
                this.id = res.data.results.ID;
                // 手动检查本地存储是否更新
                console.log('Local Storage:', localStorage.getItem('userInfo'));
            } catch (error) {
                console.error('Error fetching user info:', error);
            }
        }
    },
    persist: {
        enabled: true,
        strategies: [
            {
                key: 'userInfo', // 存储的 key，默认为 Store 的 ID
                storage: localStorage, // 存储方式，默认为 localStorage
            },
        ]
    }
});
